
/*
 * Copyright (C) 2008-2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: EnvironmentSupport.java 377 2010-03-11 20:50:18Z archie.cobbs $
 */

package org.dellroad.sidekar;

import com.sleepycat.je.EnvironmentConfig;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

/**
 * Base class for unit tests that want to create a new {@link SidekarEnvironment}
 * each time they run.
 */
public abstract class EnvironmentSupport extends TempdirSupport {

    protected SidekarEnvironment env;

    protected EnvironmentSupport() {
    }

    /**
     * This method creates the {@link SidekarEnvironment}
     * with which {@link Test}-annotated methods will run.
     */
    @BeforeClass(dependsOnMethods = "setupTempdir")
    public void setupEnvironment() {
        log.debug("setting up environment");
        this.env = new SidekarEnvironment(this.tempdir, getEnvironmentConfig());
    }

    /**
     * Clean-up method corresponding to {@link #setupEnvironment}.
     */
    @AfterClass(alwaysRun = true)
    public void cleanupEnvironment() {
        if (this.env != null) {
            this.env.close();
            this.env = null;
        }
    }

    /**
     * Get configuration for the {@link SidekarEnvironment} this test will use.
     *
     * <p>
     * This implementation just returns a default instance.
     * Subclasses should override as necessary.
     */
    protected EnvironmentConfig getEnvironmentConfig() {
        EnvironmentConfig config = new EnvironmentConfig();
        config.setAllowCreate(true);
        config.setTransactional(true);
        return config;
    }
}

